package design.simdi.code.final_paper.utils;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.wizard.IWizard;
import org.apache.wicket.extensions.wizard.IWizardModel;
import org.apache.wicket.extensions.wizard.IWizardStep;
import org.apache.wicket.markup.html.form.Form;

import design.simdi.code.final_paper.site.panels.MyWizard;

/**
 * 
 * @author Marius Dumitriu - (Jun 26, 2010 - 1:46:14 PM)
 * 
 */
public class AjaxNextButton extends WizardAjaxButton {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public AjaxNextButton(String id, IWizard wizard, Form<?> form) {
		super(id, wizard, form, "org.apache.wicket.extensions.wizard.next");

	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onClick(AjaxRequestTarget target, Form form) {
		IWizardModel wizardModel = getWizardModel();
		IWizardStep step = wizardModel.getActiveStep();

		// let the step apply any state
		step.applyState();

		// if the step completed after applying the state, move the
		// model onward
		if (step.isComplete()) {
			wizardModel.next();
			onNext(target);
		} else {
			error(getLocalizer()
					.getString("org.apache.wicket.extensions.wizard.NextButton.step.did.not.complete", this));
		}

	}

	public void onNext(AjaxRequestTarget target) {

	}

	protected void onError(final AjaxRequestTarget target, Form<?> form) {

		target.addComponent(findParent(MyWizard.class));

	}

	@Override
	public final boolean isEnabled() {
		return getWizardModel().isNextAvailable();
	}

	@Override
	protected final void onBeforeRender() {
		getForm().setDefaultButton(this);
		super.onBeforeRender();
	}
}
